//
//  StatusOriginalView.m
//  weibo-OC
//
//  Created by diyuyma on 16/3/20.
//  Copyright © 2016年 zhangkaiyun. All rights reserved.
//

#import "StatusOriginalView.h"
#import "StatusPictureView.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"

@interface StatusOriginalView ()

@property (nonatomic, strong)UIView *sepView;
@property (nonatomic, strong)UIImageView *iconImage;
@property (nonatomic, strong)UILabel *nameLable;
@property (nonatomic, strong)UIImageView *verifiedIconView;
@property (nonatomic, strong)UIImageView *memberIconView;
@property (nonatomic, strong)UILabel *timeLabel;
@property (nonatomic, strong)UILabel *sourceLabel;
@property (nonatomic, strong)UILabel *contentLabel;
@property (nonatomic, strong)StatusPictureView *pictureView;
@property (nonatomic, strong)MASConstraint *bottomConstraints;
@end

@implementation StatusOriginalView

- (void)setStatus:(Status *)status {
    [self.iconImage sd_setImageWithURL:status.user.headImageURL placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
    self.nameLable.text = status.user.name;
    self.memberIconView.image = status.user.mbrankImage;
    self.verifiedIconView.image = status.user.verified_type_Image;
    self.timeLabel.text = [[NSDate alloc] sinaDate:status.created_at].fullDescription;
    self.sourceLabel.text = status.source.linhref;
    self.contentLabel.text = status.text;
    //Masonry中 表示 移除某个约束条件
    [self.bottomConstraints uninstall];
    //给配图视图设置数据源
    NSArray *picurls = status.imageURLs;
    if (picurls.count > 0) {
        // 有配图视图
        self.pictureView.imageURLs = status.imageURLs;
        self.pictureView.hidden = false;
        // 更新约束
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            self.bottomConstraints = make.bottom.equalTo(self.pictureView.mas_bottom).offset(StatusCellMargin);
        }];
    } else {
        // 没有配图视图
        self.pictureView.hidden = true;
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            self.bottomConstraints = make.bottom.equalTo(self.contentLabel.mas_bottom).offset(StatusCellMargin);
        }];
    }
}

// MARK: 3.重写父类的构造方法 调用设置 UI
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    [self setupUI];
    return self;
}

// MARK: 2.设置 UI
- (void)setupUI {
    // 添加子控件
    self.sepView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [self addSubview:self.sepView];
    [self addSubview:self.iconImage];
    [self addSubview:self.nameLable];
    [self addSubview:self.memberIconView];
    [self addSubview:self.verifiedIconView];
    [self addSubview:self.timeLabel];
    [self addSubview:self.sourceLabel];
    [self addSubview:self.contentLabel];
    [self addSubview:self.pictureView];
    
    // 设置约束
    [self.sepView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.height.mas_equalTo(StatusCellMargin);
    }];
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sepView.mas_bottom).with.offset(StatusCellMargin);
        make.left.equalTo(self.mas_left).with.offset(StatusCellMargin);
        make.height.mas_equalTo(StatusCellIconWitdth);
        make.width.mas_equalTo(StatusCellIconWitdth);
    }];
    [self.nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImage.mas_top);
        make.left.equalTo(self.iconImage.mas_right).with.offset(StatusCellMargin);
    }];
    [self.memberIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLable.mas_top);
        make.left.equalTo(self.nameLable.mas_right).with.offset(StatusCellMargin);
    }];
    [self.verifiedIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconImage.mas_right);
        make.centerY.equalTo(self.iconImage.mas_bottom);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.iconImage.mas_bottom);
        make.left.equalTo(self.iconImage.mas_right).with.offset(StatusCellMargin);
    }];
    [self.sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.timeLabel.mas_bottom);
        make.left.equalTo(self.timeLabel.mas_right).with.offset(StatusCellMargin);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImage);
        make.top.equalTo(self.iconImage.mas_bottom).with.offset(StatusCellMargin);
    }];
    [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).with.offset(StatusCellMargin);
        make.left.equalTo(self.contentLabel.mas_left);
    }];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        //将约束条件转换成 约束类型
       self.bottomConstraints = make.bottom.equalTo(self.pictureView.mas_bottom).with.offset(StatusCellMargin);
    }];
}

// MARK: 1.懒加载所有子控件
- (UIView *)sepView {
    if (_sepView == nil) {
        _sepView = [[UIView alloc] init];
    }
    return _sepView;
}

- (UIImageView *)iconImage {
    
    if (_iconImage == nil) {
        _iconImage = [[UIImageView alloc] init];//WithImage:[UIImage imageNamed:@"avatar_default_big"]];
        CALayer * l = [_iconImage layer];
        [l setMasksToBounds:YES];
        [l setCornerRadius:17.0];
    }
    return _iconImage;
}

- (UILabel *)nameLable {
    if (_nameLable == nil) {
        _nameLable = [[UILabel alloc] init];
        [_nameLable initWithLabel:@"ad" WithColor:[UIColor orangeColor] WithSize:14 WithMargin:0];
    }
    return _nameLable;
}

- (UIImageView *)verifiedIconView {
    if (_verifiedIconView == nil) {
        _verifiedIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"avatar_vip"]];
    }
    return _verifiedIconView;
}

- (UIImageView *)memberIconView {
    if (_memberIconView == nil) {
        _memberIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_icon_membership_level1"]];
    }
    return _memberIconView;
}

- (UILabel *)timeLabel {
    if (_timeLabel == nil) {
        _timeLabel = [[UILabel alloc] init];
        [_timeLabel initWithLabel:@"11:11" WithColor:[UIColor orangeColor] WithSize:10 WithMargin:0];
    }
    return _timeLabel;
}

- (UILabel *)sourceLabel {
    if (_sourceLabel == nil) {
        _sourceLabel = [[UILabel alloc] init];
        [_sourceLabel initWithLabel:@"来自: 一道光" WithColor:[UIColor lightGrayColor] WithSize:10 WithMargin:0];
    }
    return _sourceLabel;
}

- (UILabel *)contentLabel {
    if (_contentLabel == nil) {
        _contentLabel = [[UILabel alloc] init];
        [_contentLabel initWithLabel:@"野内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮内心的攻城狮" WithColor:[UIColor lightGrayColor] WithSize:14 WithMargin:StatusCellMargin];
    }
    return _contentLabel;
}

- (StatusPictureView *)pictureView {
    if (_pictureView == nil) {
        _pictureView = [[StatusPictureView alloc] init];
    }
    return _pictureView;
}

//原创微博底部约束  用来记录底部约束  后续根据是否有配图 来动态修改
//- (MASConstraint *)bottomConstraints {
//    if (_bottomConstraints == nil) {
//        _bottomConstraints = [[MASConstraint alloc] init];
//    }
//    return _bottomConstraints;
//}

@end
